package com.block.datapush.service.filter;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.common.model.dto.FlowCountDTO;
import com.block.common.service.IChannelCommonService;
import com.block.common.service.IMerchantCommonService;
import com.block.common.util.BigDecimalUtil;
import com.block.datapush.model.param.FilterHandlerParam;
import com.block.datapush.model.result.FilterHandlerResult;
import com.block.datapush.service.IDataFilterItemService;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class FilterSourceDataScaleHandlerService implements IDataFilterItemService {

    @Autowired
    IMerchantCommonService merchantCommonService;
    @Autowired
    IChannelCommonService channelCommonService;

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberSourceInfo memberSourceInfo) {

        // 如果是渠道量，则直接可以推送
        ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(memberSourceInfo.getChannelCode());
        if (StrUtil.equals(channelBaseInfo.getFlowRateType(), "APP")||(StrUtil.equals(channelBaseInfo.getFlowRateType(), "WX") || StrUtil.equals(channelBaseInfo.getFlowRateType(), "H5"))) {
            return FilterHandlerResult.success();
        }


        FlowCountDTO cacheFlowCount = merchantCommonService.getCacheFlowCount(handlerParam.getInfoId());

        MerchantBaseInfo merchantBaseInfo = merchantCommonService.getMerchantBaseInfo(handlerParam.getInfoId());

        if (new BigDecimal(-1).compareTo(merchantBaseInfo.getSourceDataScale()) == 0) {
            return FilterHandlerResult.success();
        }

        if (ObjectUtil.hasEmpty(merchantBaseInfo, cacheFlowCount)
                || ObjectUtil.hasEmpty(merchantBaseInfo.getSourceDataScale(),
                cacheFlowCount.getAppNumber(), cacheFlowCount.getH5Number(), cacheFlowCount.getApibNumber(), cacheFlowCount.getApisNumber())
        ) {
            // 如果有空值，则不进行控制
            return FilterHandlerResult.success();
        }

        BigDecimal channelNumber = BigDecimalUtil.add(new BigDecimal(cacheFlowCount.getAppNumber()), new BigDecimal(cacheFlowCount.getH5Number()));
        BigDecimal sourceNumber = BigDecimalUtil.add(new BigDecimal(cacheFlowCount.getApibNumber()), new BigDecimal(cacheFlowCount.getApisNumber()));

        BigDecimal canSourceDataNumber = channelNumber.multiply(merchantBaseInfo.getSourceDataScale()).setScale(0, RoundingMode.HALF_DOWN);
        if (canSourceDataNumber.compareTo(sourceNumber) > 0) {
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【channelNumber】{}【sourceDataScale】{}【canSourceDataNumber】{}【sourceNumber】{}",
                channelNumber, merchantBaseInfo.getSourceDataScale(), canSourceDataNumber, sourceNumber);
        return FilterHandlerResult.error("SOURCE_FLOW_ERROR", message);
    }
}
